/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import preferences.PreferencesModel;

/**
 *
 * @author Carmen
 */
public class PreferencesView extends JDialog implements ActionListener {
    
    /* General fields */
    private JComboBox _cmbMainModule;
    private JTextField _txtMainWidth;
    private JTextField _txtMainHeight;
    
    /* Navigator fields */
    private JTextField _txtBrowserDefaultHomePage;
    private JTextField _txtBrowserMaxTabNumber;
    private JTextField _txtBrowserMaxTabLenght;
    
    /* Multimedia fields */
    private JTextField _txtMusicFolder;
    
    /* Explorer fields */
    private JTextField _txtExplorerDefaultFolder;
    
    /* RSS fields */
    private JTextField _txtRssListFeedAddress;
    
    
    /* Validations buttons */
    private JButton _btnOk;
    private JButton _btnCancel;
    
    /* Model */
    private PreferencesModel _model = new PreferencesModel();

    public PreferencesView() {
        this(null);
    }
    
    public PreferencesView(JWindow parent) {
        super(parent);
        
        setModal(true);
        setTitle("Mes préférences");
        setSize(500, 175);
        //setResizable(false);
        setLayout(new BorderLayout());
        setLocationRelativeTo(parent);
        setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        
        add(getMainContent(), BorderLayout.CENTER);
        add(getValidationContent(), BorderLayout.SOUTH);
        
        setVisible(true);
    }
 
    private JTabbedPane getMainContent() {
        
        JTabbedPane tp = new JTabbedPane();
        
        tp.addTab("Général", getGlobalContent());
        tp.addTab("Navigateur", getNavigatorContent());
        tp.addTab("Lecteur multimédia", getMultimediaContent());
        tp.addTab("Explorateur", getExplorerContent());
        tp.addTab("Lecteur RSS", getRSSReaderContent());
        
        return tp;
    }
    
    public JPanel getValidationContent() {
        
        JPanel p = new JPanel();
        
        _btnOk = new JButton("Valider");
        _btnCancel = new JButton("Annuler");
        //_btnApply = new JButton("Appliquer");
        
        _btnOk.addActionListener(this);
        _btnCancel.addActionListener(this);
        //_btnApply.addActionListener(this);
        
        p.add(_btnOk);
        p.add(_btnCancel);
        //p.add(_btnApply);
        
        return p;
    }
    
    public JPanel getGlobalContent() {
        
        JPanel p = new JPanel();
        
        /* Need modifications here to select the default module and get back the selected one in ActionListener */
        JPanel pModule = new JPanel();
        _cmbMainModule = new JComboBox();
        _cmbMainModule.addItem("Navigateur");
        _cmbMainModule.addItem("Lecteur multimédia");
        _cmbMainModule.addItem("Explorateur de fichier");
        _cmbMainModule.addItem("Lecteur de flux RSS");
        pModule.add(new JLabel("Module au démarrage de l'application"));
        pModule.add(_cmbMainModule);
        
        JPanel pWidth = new JPanel();
        _txtMainWidth = new JTextField(String.valueOf(_model.getMainWidth()));
        _txtMainWidth.setPreferredSize(new Dimension(40, 20));
        pWidth.add(new JLabel("Largeur de fenêtre"));
        pWidth.add(_txtMainWidth);
        
        JPanel pHeight = new JPanel();
        _txtMainHeight = new JTextField(String.valueOf(_model.getMainHeight()));
        _txtMainHeight.setPreferredSize(new Dimension(40, 20));
        pHeight.add(new JLabel("Hauteur de fenêtre"));
        pHeight.add(_txtMainHeight);
        
        p.add(pModule);
        p.add(pWidth);
        p.add(pHeight);
        
        return p;
    }
    
    public JPanel getNavigatorContent() {
        JPanel p = new JPanel();

        JPanel pHomePage = new JPanel();
        _txtBrowserDefaultHomePage = new JTextField(_model.getBrowserDefaultHomePage());
        _txtBrowserDefaultHomePage.setPreferredSize(new Dimension(200, 20));
        pHomePage.add(new JLabel("Page par défaut du navigateur"));
        pHomePage.add(_txtBrowserDefaultHomePage);
        
        JPanel pTabMaxNumber = new JPanel();
        _txtBrowserMaxTabNumber = new JTextField(_model.getBrowserMaxTabLenght());
        _txtBrowserMaxTabNumber.setPreferredSize(new Dimension(40, 20));
        pTabMaxNumber.add(new JLabel("Nombre d'onglets maximum"));
        pTabMaxNumber.add(_txtBrowserMaxTabNumber);
        
        JPanel pTabMaxLenght = new JPanel();
        _txtBrowserMaxTabLenght = new JTextField(_model.getBrowserMaxTabLenght());
        _txtBrowserMaxTabLenght.setPreferredSize(new Dimension(40, 20));
        pTabMaxLenght.add(new JLabel("Longueur du titre des onglets"));
        pTabMaxLenght.add(_txtBrowserMaxTabLenght);
        
        p.add(pHomePage);
        p.add(pTabMaxNumber);
        p.add(pTabMaxLenght);
        
        return p;
    }
    
    public JPanel getMultimediaContent() {
        JPanel p = new JPanel();
        
        JPanel pFolder = new JPanel();
        _txtMusicFolder = new JTextField(_model.getMusicFolder());
        _txtMusicFolder.setPreferredSize(new Dimension(200, 20));
        pFolder.add(new JLabel("Répertoire musical"));
        pFolder.add(_txtMusicFolder);
        
        p.add(pFolder);
        
        return p;
    }
    
    public JPanel getExplorerContent() {
        JPanel p = new JPanel();
        
        JPanel pFolder = new JPanel();
        _txtExplorerDefaultFolder = new JTextField(_model.getExplorerDefaultFolder());
        _txtExplorerDefaultFolder.setPreferredSize(new Dimension(200, 20));
        pFolder.add(new JLabel("Racine de l'ordinateur"));
        pFolder.add(_txtExplorerDefaultFolder);
        
        p.add(pFolder);
        
        return p;
    }
    
    public JPanel getRSSReaderContent() {
        JPanel p = new JPanel();
        
        JPanel pAddress = new JPanel();
        _txtRssListFeedAddress = new JTextField(_model.getRssListFeedAddress());
        _txtRssListFeedAddress.setPreferredSize(new Dimension(200, 20));
        pAddress.add(new JLabel("Chemin vers le fichier d'abonnements"));
        pAddress.add(_txtRssListFeedAddress);
        
        p.add(pAddress);
        
        return p;
    }

    /**
     * @return the _model
     */
    public PreferencesModel getModel() {
        return _model;
    }

    /**
     * @param model the _model to set
     */
    public void setModel(PreferencesModel model) {
        this._model = model;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        
//    /* General fields MISSED !!! */
//    JComboBox _cmbMainModule;

        
        JButton btn = (JButton) e.getSource();
        
        // We save on module the good informations
        if (btn == _btnOk) {
            
            String tmpStr;
            int tmpInt;
            
            // Main Width
            try {
                tmpInt = Integer.parseInt(_txtMainWidth.getText());
                if (tmpInt != 0) {
                    _model.setMainWidth(tmpInt);
                }
            } catch (NumberFormatException ex) {
            }
            
            // Main Height
            try {
                tmpInt = Integer.parseInt(_txtMainHeight.getText());
                if (tmpInt != 0) {
                    _model.setMainHeight(tmpInt);
                }
            } catch (NumberFormatException ex) {
            }
            
            // Browser home page
            tmpStr = _txtBrowserDefaultHomePage.getText();
            if (!tmpStr.isEmpty()) {
                _model.setBrowserDefaultHomePage(tmpStr);
            }
            
            // Browser max tab number
            // Main Height
            try {
                tmpInt = Integer.parseInt(_txtBrowserMaxTabNumber.getText());
                if (tmpInt != 0) {
                    _model.setBrowserMaxTabNumber(tmpInt);
                }
            } catch (NumberFormatException ex) {
            }
            
            // Browser max tab lenght
            // Main Height
            try {
                tmpInt = Integer.parseInt(_txtBrowserMaxTabLenght.getText());
                if (tmpInt != 0) {
                    _model.setBrowserMaxTabLenght(tmpInt);
                }
            } catch (NumberFormatException ex) {
            }
            
            // Music folder
            tmpStr = _txtMusicFolder.getText();
            if (!tmpStr.isEmpty()) {
                _model.setMusicFolder(tmpStr);
            }
            
            // Explorer root folder
            tmpStr = _txtExplorerDefaultFolder.getText();
            if (!tmpStr.isEmpty()) {
                _model.setExplorerDefaultFolder(tmpStr);
            }
            
            // Rss address file
            tmpStr = _txtRssListFeedAddress.getText();
            if (!tmpStr.isEmpty()) {
                _model.setRssListFeedAddress(tmpStr);
            }
            
            System.out.println(_model);
        }
        
        this.dispose();
    }
    
}
